package cn.zxh.improvement.gencode.processor.service;

import cn.zxh.improvement.gencode.processor.BaseCodeGenProcessor;
import cn.zxh.improvement.gencode.spi.CodeGenProcessor;
import cn.zxh.improvement.gencode.util.GenCodeUtil;
import com.google.auto.service.AutoService;
import com.squareup.javapoet.TypeSpec;

import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
import java.lang.annotation.Annotation;

/**
 * serviceI 接口生成器
 *
 * @author zxh
 * @since 2023/5/6 16:54:03
 */
@AutoService(value = CodeGenProcessor.class)
public class GenServiceProcessor extends BaseCodeGenProcessor {

    public static final String SUFFIX = "Service";

    public static final String PREFIX = "I";


    @Override
    protected void generateClass(TypeElement typeElement, RoundEnvironment roundEnvironment) {
        String className = PREFIX + typeElement.getSimpleName() + SUFFIX;
        TypeSpec.Builder typeSpecBuilder = TypeSpec.interfaceBuilder(className)
                .addModifiers(Modifier.PUBLIC);
        GenCodeUtil.genJavaSourceFile(generatePackage(typeElement), typeElement.getAnnotation(GenService.class).sourcePath(), typeSpecBuilder.build());
    }

    @Override
    public Class<? extends Annotation> getAnnotation() {
        return GenService.class;
    }

    @Override
    public String generatePackage(TypeElement typeElement) {
        return typeElement.getAnnotation(GenService.class).pkgName();
    }
}
